/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubUser;
import com.thinkgem.jeesite.modules.pub.service.PubUserService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubUser")
public class PubUserController extends BaseController {

	@Autowired
	private PubUserService pubUserService;
	
	@ModelAttribute
	public PubUser get(@RequestParam(required=false) String id) {
		PubUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubUserService.get(id);
		}
		if (entity == null){
			entity = new PubUser();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubUser pubUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubUser> page = pubUserService.findPage(new Page<PubUser>(request, response), pubUser); 
		model.addAttribute("page", page);
		return "modules/pub/pubUserList";
	}

	@RequiresPermissions("pub:pubUser:view")
	@RequestMapping(value = "form")
	public String form(PubUser pubUser, Model model) {
		model.addAttribute("pubUser", pubUser);
		return "modules/pub/pubUserForm";
	}

	@RequiresPermissions("pub:pubUser:edit")
	@RequestMapping(value = "save")
	public String save(PubUser pubUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubUser)){
			return form(pubUser, model);
		}
		pubUserService.save(pubUser);
		addMessage(redirectAttributes, "保存用户管理成功");
		return "modules/pub/pubUserList";
	}
	
	@RequiresPermissions("pub:pubUser:edit")
	@RequestMapping(value = "delete")
	public String delete(PubUser pubUser, RedirectAttributes redirectAttributes) {
		pubUserService.delete(pubUser);
		addMessage(redirectAttributes, "删除用户管理成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubUser/?repage";
	}
	
	@RequestMapping(value = "wxlogin")
	public String wxlogin(HttpServletRequest request, HttpServletResponse response,String userno,String password) {
		String psd = pubUserService.getPsd(userno);
		Map<String, String> map = new HashMap<String, String>();
		PubUser entity = null;
		if(psd.equals(password)) {
			map.put("result", "true");
			map.put("msg", "登录成功");
			
			if (StringUtils.isNotBlank(userno)){
				entity = pubUserService.getInfo(userno);
			}
			if (entity == null){
				entity = new PubUser();
			}
        	
		}else {
			map.put("result", "false");
			map.put("msg", "登录失败");
		}
		return renderString(response, entity);
		
	}
	@RequestMapping(value = {"wxregister"})
	public String wxregister(HttpServletRequest request, HttpServletResponse response,String username,String password,String userno,String tel,String age,String birth,String job) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = pubUserService.register(id,username,password,userno,tel,age,job,birth);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","注册成功");
		}else {
			map.put("result","false");
			map.put("msg","注册失败");
		}
		return renderString(response, map);

	}
	@RequestMapping(value = {"wxupdate"})
	public String wxupdate(HttpServletRequest request, HttpServletResponse response,String id,String username,String password,String userno,String tel,String age,String birth,String job) {
		int result = pubUserService.updateUser(id,username,password,userno,tel,age,job,birth);
		Map<String, String> map = new HashMap<String, String>();
		PubUser entity = null;
		if (result>0) {
			
			if (StringUtils.isNotBlank(id)){
				entity = pubUserService.get(id);
			}
			if (entity == null){
				entity = new PubUser();
			}

		}
		return renderString(response, entity);

	}
	
}